﻿using System;
using System.Collections;
using System.Data;
using Lib.Data;
using Lib.Utils;


namespace SysObj
{
    public class Bh_BanHang_GopBan : IDisposable
    {
        DataLayer m_DataLayer;
        public Bh_BanHang_GopBan()
        {
            m_DataLayer = new DataLayer();
        }

        public void Dispose()
        {
            try
            {
                if (this.m_DataLayer != null)
                {
                    this.m_DataLayer.Dispose();
                }
                this.m_DataLayer = null;
            }
            catch
            {
            }
        }

        public void Merge(int ID1, int ID2)
        {
            m_DataLayer.ExecNonQuery("Sp_Bh_BanHang_GopBan", "Command,IdBanHang1,IdBanHang2", "Merge", ID1, ID2);
        }

        public void UnMerge(int ID)
        {
            m_DataLayer.ExecNonQuery("Sp_Bh_BanHang_GopBan", "Command,IdBanHang1", "UnMerge", ID);
        }

        public DataTable LoadMergeItemByID(int ID)
        {
            return m_DataLayer.ExecDataTable("Sp_Bh_BanHang_GopBan", "Command,IdBanHang1", "LoadMergeItemByID", ID);
        }

        /// <summary>
        /// Return New Sale ID
        /// </summary>
        /// <param name="fromSaleId"></param>
        /// <param name="fromTableId"></param>
        /// <param name="toTableId"></param>
        /// <param name="Source"></param>
        /// <returns></returns>
        public int MoveOrSplit(int fromSaleId, int fromTableId, int toTableId, DataTable Source)
        {
            int new_id = 0;
            try
            {
                int pt_ck = 0;
                decimal total = 0;
                using (SysObj.Bh_BanHang bh = new Bh_BanHang())
                {
                    bh.IdBanHang = fromSaleId;
                    bh.LoadData();
                    pt_ck = bh.PT_CK;
                }
                foreach (DataRow ro in Source.Rows)
                {
                    total = total + Cvert.ToDecimal(ro["SoLuong"], 0) * Cvert.ToDecimal(ro["DonGia"], 0);
                    total = total - (total * Cvert.ToDecimal(ro["PT_CK"], 0) / 100);
                }

                m_DataLayer.BeginTransaction();

                //Make new table
                using (SysObj.Bh_BanHang bh = new Bh_BanHang())
                {
                    bh.m_DataLayer = this.m_DataLayer;
                    bh.NoneTransaction = true;
                    bh.ResetData();

                    bh.IdBan = toTableId;
                    bh.PT_CK = pt_ck;
                    bh.Tien_CK = total * pt_ck / 100;
                    bh.ThanhTien = total;
                    bh.TongTien = total - bh.Tien_CK;
                    bh.Ngay = Cvert.ToShortDate(DateTime.Now);
                    bh.D_HH = Source;

                    bh.OnUpdate();

                    new_id = bh.IdBanHang;
                }

                total = 0;
                //Remove detail on old table
                using (SysObj.Bh_BanHang bh = new Bh_BanHang())
                {
                    bh.m_DataLayer = this.m_DataLayer;
                    bh.NoneTransaction = true;

                    bh.IdBanHang = fromSaleId;
                    bh.LoadData();

                    DataTable tmp = bh.GetDetail();

                    foreach (DataRow ro in Source.Rows)
                    {
                        DataRow[] rS = tmp.Select("IdHangHoa=" + Cvert.ToInt32(ro["IdHangHoa"], 0));
                        if (!object.Equals(rS, null) && rS.Length > 0)
                            tmp.Rows.Remove(rS[0]);
                        tmp.AcceptChanges();
                    }

                    foreach (DataRow r in tmp.Rows)
                    {
                        total = total + Cvert.ToDecimal(r["SoLuong"], 0) * Cvert.ToDecimal(r["DonGia"], 0);
                        total = total - (total * Cvert.ToDecimal(r["PT_CK"], 0) / 100);
                    }
                    bh.Tien_CK = total * pt_ck / 100;
                    bh.ThanhTien = total;
                    bh.TongTien = total - bh.Tien_CK;
                    bh.D_HH = tmp;
                    bh.OnUpdate();
                }

                m_DataLayer.CommitTransaction();
            }
            catch (Exception ex)
            {
                m_DataLayer.RollbackTransaction();
                throw ex;
            }

            return new_id;
        }

        public void MergeDetail(int fromSaleID, int toSaleID, DataTable Source, bool MergeAll)
        {
            try
            {
                decimal sub = 0;
                m_DataLayer.BeginTransaction();

                //Insert menu into toSale
                using (SysObj.Bh_BanHang bh = new Bh_BanHang())
                {
                    bh.m_DataLayer = this.m_DataLayer;
                    bh.NoneTransaction = true;

                    bh.IdBanHang = toSaleID;
                    bh.LoadData();

                    DataTable tmp = bh.GetDetail();
                    foreach (DataRow ro in Source.Rows)
                    {
                        sub = sub + Cvert.ToDecimal(ro["SoLuong"], 0) * Cvert.ToDecimal(ro["DonGia"], 0);
                        sub = sub - (sub * Cvert.ToDecimal(ro["PT_CK"], 0) / 100);

                        //Check menu is existed
                        DataRow[] rS = tmp.Select("IdHangHoa=" + Cvert.ToInt32(ro["IdHangHoa"], 0));
                        if (!object.Equals(rS, null) && rS.Length > 0)
                        {
                            int pt_ck = 0;
                            if(Cvert.ToInt32(rS[0]["PT_CK"], 0)==0)
                                pt_ck = Cvert.ToInt32(ro["PT_CK"], 0);

                            rS[0]["SoLuong"] = Cvert.ToDecimal(rS[0]["SoLuong"], 0) + Cvert.ToDecimal(ro["SoLuong"], 0);
                            rS[0]["ThanhTien"] = Cvert.ToDecimal(rS[0]["SoLuong"], 0) * Cvert.ToDecimal(rS[0]["DonGia"], 0);
                            rS[0]["Tien_CK"] = Cvert.ToDecimal(rS[0]["ThanhTien"], 0) * pt_ck / 100;
                            rS[0]["TongTien"] = Cvert.ToDecimal(rS[0]["ThanhTien"], 0) + Cvert.ToDecimal(rS[0]["Tien_CK"], 0);
                        }
                        else
                            tmp.ImportRow(ro);
                    }
                    bh.ThanhTien += sub;
                    bh.TongTien = bh.ThanhTien - bh.ThanhTien * bh.PT_CK / 100;
                    bh.D_HH = tmp;
                    bh.OnUpdate();
                }

                //Remove menu in fromSale
                using (SysObj.Bh_BanHang bh = new Bh_BanHang())
                {
                    bh.m_DataLayer = this.m_DataLayer;
                    bh.NoneTransaction = true;

                    bh.IdBanHang = fromSaleID;

                    if (!MergeAll)
                    {
                        foreach (DataRow ro in Source.Rows)
                        {
                            bh.OnRemoveMenu(Cvert.ToInt32(ro["IdHangHoa"], 0));
                        }

                        bh.LoadData();
                        DataTable tmp = bh.GetDetail();

                        bh.ThanhTien = bh.ThanhTien - sub;
                        bh.TongTien = bh.ThanhTien - bh.ThanhTien * bh.PT_CK / 100;
                        bh.D_HH = tmp;

                        bh.OnUpdate();
                    }
                    else
                        bh.OnDelete();
                }


                m_DataLayer.CommitTransaction();
            }
            catch (Exception ex)
            {
                m_DataLayer.RollbackTransaction();
                throw ex;
            }
        }
    }
}
